/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dtn.wsn.dao;

import com.dtn.wsn.database.ConnectionPool;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *Data Access object for sunspot entity
 * @author hao
 */
public class SpotDAO {
    private ConnectionPool connectionPool;

    public SpotDAO(ConnectionPool connectionPool) {
        this.connectionPool = connectionPool;
    }
    /**
     * check whether sunspot is in the database
     * @param mac_addr
     * @return 
     */
    public int isExsist(String mac_addr){
        int ret_val = -1;
        String sql = "select spot_id from sunspot where spot_mac_addr = '"+ mac_addr +"'";
        
        try {
            Connection cn = connectionPool.getConnection();
            
            Statement stmt = cn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            if(rs.next()){
                ret_val = rs.getInt("spot_id");
            }
            connectionPool.freeConnection(cn);
        } catch (SQLException ex) {
            Logger.getLogger(DataDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return ret_val;
    }
    /**
     * insert spot into database
     * @param mac_addr
     * @return 
     */
    public int insertSpot(String mac_addr){
        
        int insertRet_val = 0;
        String sql = "INSERT INTO sunspot(spot_mac_addr) values"
                + "('"+ mac_addr +"')";
        try {
            Connection cn = connectionPool.getConnection();
            
            Statement stmt = cn.createStatement();
            insertRet_val = stmt.executeUpdate(sql);
            
            connectionPool.freeConnection(cn);
        } catch (SQLException ex) {
            Logger.getLogger(DataDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return insertRet_val;
    }
}
